document.getElementById('calculate').addEventListener('click', function() {
    const totalAnimals = parseInt(document.getElementById('total-animals').value, 10);
    const totalLegs = parseInt(document.getElementById('total-legs').value, 10);
    const result = solveChickenRabbit(totalAnimals, totalLegs);
    document.getElementById('result').textContent = result.message;
    if (result.chickens !== undefined && result.rabbits !== undefined) {
      document.getElementById('result').style.color = 'blue';
      document.getElementById('result').style.fontWeight = 'bold';
    } else {
      document.getElementById('result').style.color = 'red';
      document.getElementById('result').style.fontWeight = 'bold';
    }
  });

  function solveChickenRabbit(totalAnimals, totalLegs) {
    for (let i = 0; i <= totalAnimals; i++) {
      const j = totalAnimals - i;
      if (2 * i + 4 * j === totalLegs && i + j === totalAnimals) {
        return { message: `鸡：${i}只，兔：${j}只`, chickens: i, rabbits: j };
      }
    }
    return { message: '无解' };
  }